package jp.syuriken.snsw.terenaibot.mal.twitter;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jp.syuriken.snsw.terenaibot.Post;
import jp.syuriken.snsw.terenaibot.Post2;
import jp.syuriken.snsw.terenaibot.PostType;
import twitter4j.DirectMessage;
import twitter4j.TwitterResponse;
import twitter4j.User;

/**
 * {@link twitter4j.Status}を使用した{@link Post}。
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $
 */
public class TwitterPost extends Post2 {
	
	/** TODO snsoftware */
	protected static final String POST_CLASS_NAME = "TwitterPost";
	
	protected String postId;
	
	protected String postText;
	
	protected String postUser;
	
	protected String inReplyToUserId;
	
	protected String inReplyToPostId;
	
	protected String source;
	
	protected Date createdAt;
	
	protected TwitterResponse original;
	
	protected String postUserName;
	
	protected PostType postType;
	
	protected TwitterUserInfo postUserInfo;
	
	protected String postActualString;
	
	private boolean isRetweet;
	
	/** 送信先が単一と判断するパターン */
	public static final String UNICAST_PATTERN = "^@{{ScreenName}}\\s*([^@]+)$";
	
	public static final int[] UNICAST_ACTUAL_DATA_GROUP = new int[] {
		1
	};
	
	/** 送信先が複数と判断するパターン */
	public static final String MULTICAST_PATTERN = "@{{ScreenName}}\\s*([^@]+)";
	
	public static final int[] MULTICAST_ACTUAL_DATA_GROUP = new int[] {
		1
	};
	

	/**
	 * インスタンスを生成する。
	 * 
	 * @param directMessage DirectMessage
	 */
	public TwitterPost(DirectMessage directMessage) {
		postId = String.valueOf(directMessage.getId());
		postText = directMessage.getText();
		createdAt = directMessage.getCreatedAt();
		inReplyToPostId = null;
		inReplyToUserId = null;
		source = "";
		isRetweet = false;
		
		User user = directMessage.getSender();
		postUser = String.valueOf(user.getId());
		postUserName = user.getScreenName();
		
		original = directMessage;
	}
	
	/**
	 * {@link twitter4j.Status}を使用してインスタンスを生成する。
	 * 
	 * @param status ステータスインスタンス。
	 * @param screenName スクリーン名。送信先の識別に使用されます。
	 */
	public TwitterPost(twitter4j.Status status, String screenName) {
		postId = String.valueOf(status.getId());
		postText = status.getText();
		createdAt = status.getCreatedAt();
		inReplyToUserId = status.getInReplyToScreenName();
		inReplyToPostId = String.valueOf(status.getInReplyToStatusId());
		source = status.getSource();
		isRetweet = status.isRetweet();
		
		User user = status.getUser();
		postUser = String.valueOf(user.getId());
		postUserName = user.getScreenName();
		postUserInfo = new TwitterUserInfo(user);
		
		original = status;
		
		Matcher unicastMatcher =
				Pattern.compile(UNICAST_PATTERN.replace("{{ScreenName}}", screenName)).matcher(postText);
		Matcher multicastMatcher =
				Pattern.compile(MULTICAST_PATTERN.replace("{{ScreenName}}", screenName)).matcher(postText);
		if (unicastMatcher.matches()) {
			postType = PostType.UNICAST;
			StringBuilder stringBuilder = new StringBuilder();
			for (int i : UNICAST_ACTUAL_DATA_GROUP) {
				stringBuilder.append(unicastMatcher.group(i));
			}
			postActualString = stringBuilder.toString();
		} else if (multicastMatcher.matches()) {
			postType = PostType.MULTICAST;
			StringBuilder stringBuilder = new StringBuilder();
			for (int i : MULTICAST_ACTUAL_DATA_GROUP) {
				stringBuilder.append(multicastMatcher.group(i));
			}
			postActualString = stringBuilder.toString();
		} else {
			postType = PostType.DEFAULT;
			postActualString = postText;
		}
		
	}
	
	@Override
	public Date getCreatedAt() {
		return (Date) createdAt.clone();
	}
	
	@Override
	public String getInReplyToPostId() {
		return inReplyToPostId;
	}
	
	@Override
	public String getInReplyToUserId() {
		return inReplyToUserId;
	}
	
	@Override
	public TwitterResponse getOriginal() {
		return original;
	}
	
	@Override
	public String getPostActualText() {
		return postActualString;
	}
	
	@Override
	public String getPostClassName() {
		return POST_CLASS_NAME;
	}
	
	@Override
	public String getPostId() {
		return postId;
	}
	
	@Override
	public String getPostText() {
		return postText;
	}
	
	@Override
	public PostType getPostType() {
		return postType;
	}
	
	@Override
	public String getSource() {
		return source;
	}
	
	@Override
	public String getUser() {
		return postUser;
	}
	
	@Override
	public String getUserName() {
		return postUserName;
	}
	
	@Override
	public boolean isRepost() {
		return isRetweet;
	}
	
}
